//---------------------------------------------------------------------------

#ifndef CIISServerInterfaceH
#define CIISServerInterfaceH

#include "CIISDB.h"
#include "DebugWinU.h"
#include "CIIClientSocket.h"

//---------------------------------------------------------------------------
enum CIISEvTabStatus { ETSMissingRec, ETSPrjCh, ETSCtrlCh, ETSZoneCh, ETSPSCh,
                      ETSRecCh, ETSSensorCh };
enum CIISPrjTabStatus { PTSLocalError, PTSMissingRec, PTSConfigCh, PTSDataCh,
                        PTSLastValCh, PTSPrjNoDontMatch };

enum CIISCtrlTabStatus { CTSLocalError, CTSMissingRec };

enum CIISZoneTabStatus { ZTSLocalError, ZTSMissingRec };

enum CIISSensTabStatus { STSLocalError, STSMissingRec };

enum CIISPSTabStatus { PSTSLocalError, PSTSMissingRec };

enum CIISRecTabStatus { RecTSLocalError, RecTSMissingRec };

enum CIISValTabStatus { ValTSLocalError, ValTSMissingRec };

enum CIISLPRTabStatus { LPRTSLocalError, LPRTSMissingRec };

enum CIISDecayTabStatus { DecayTSLocalError, DecayTSMissingRec };

class TCIISServerInterface : public TObject
{
private:
  TCIISDBModule *DB;
  TDebugWin *DW;
  TransferReplyFP TransferReply;
  int PortNo;
  String IPAdr;
  TList *TransferMessageList, *TransferResponseList;
  TCIIClientSocket *ClientSocket;
	Byte MessageNumber;
	TTable *TabSync;
	String UserLevel, Password;
	bool FLoggedIn;
	int ValueFirstSyncRecord, DecayFirstSyncRecord, LPRFirstSyncRecord;
	int ValueLastSyncRecord, DecayLastSyncRecord, LPRLastSyncRecord;
	int ValueCurrSyncRecord, DecayCurrSyncRecord, LPRCurrSyncRecord;
	int SyncReadyValues, SyncReadyDecay, SyncReadyLPR;
	bool ValueFirstRead, DecayFirstRead, LPRFirstRead;
	int LoggerDBVer;

	String Rec, Sensor, LPRStep;
	TDateTime RecStartTime, DTS;
	double LPRInterval;

	TTimer *TimerSM;
	TTimer *TimerSMCancel;
  
  CIISCheckSyncState CheckSyncState;
  CIISDataSyncState DataSyncState;
  CIISSysSyncState SysSyncState;
  CIISEventSyncState EventSyncState;
  CIISProjectSyncState ProjectSyncState;
  CIISCtrlSyncState CtrlSyncState;
  CIISZoneSyncState ZoneSyncState;
  CIISSensorSyncState SensSyncState;
  CIISPSSyncState PSSyncState;
  CIISRecSyncState RecSyncState;
  CIISValSyncState ValSyncState;
  CIISLPRSyncState LPRSyncState;
  CIISDecaySyncState DecaySyncState;
	CIISLastValSyncState LastValSyncState;
	CIISLoggInState LoggInState;
	CIISOpenComState OpenComState;
	CIISCloseComState CloseComState;
	CIISCancelSyncState CancelSyncState;

  String LastSensor, LastPS;

  TDateTime TOut;
  bool RequestTimeOut;
  int RequestNo;
	bool FSysSyncRunning, FDataSyncRunning, FTransferMessageRunning, FLoggInRunning,
			 FCancelSyncRunning, FOpenComRunning, FCloseComRunning;
	bool SysSyncCancel, DataSyncCancel, SendTransferMessageCancel, LoggInCancel,
			 CancelSyncCancel, OpenComCancel, CloseComCancel;
	bool ValuesTabSyncCancel, ValuesLPRTabSyncCancel, ValuesDecayTabSyncCancel;
	bool EventTabSyncCancel, ZoneTabSyncCancel, SensorTabSyncCancel,	PSTabSyncCancel,
			 RecordingTabSyncCancel;


	TLocateOptions NoOpts;

  Set < CIISEvTabStatus, ETSMissingRec, ETSSensorCh > EvTabStatusSet;
  Set < CIISPrjTabStatus, PTSLocalError, PTSPrjNoDontMatch > PrjTabStatusSet;
  Set < CIISCtrlTabStatus, CTSLocalError, CTSMissingRec > CtrlTabStatusSet;
  Set < CIISZoneTabStatus, ZTSLocalError, ZTSMissingRec > ZoneTabStatusSet;
  Set < CIISSensTabStatus, STSLocalError, STSMissingRec > SensTabStatusSet;
  Set < CIISPSTabStatus, PSTSLocalError, PSTSMissingRec > PSTabStatusSet;
  Set < CIISRecTabStatus, RecTSLocalError, RecTSMissingRec > RecTabStatusSet;
  Set < CIISValTabStatus, ValTSLocalError, ValTSMissingRec > ValTabStatusSet;
  Set < CIISLPRTabStatus, LPRTSLocalError, LPRTSMissingRec > LPRTabStatusSet;
  Set < CIISDecayTabStatus, DecayTSLocalError, DecayTSMissingRec > DecayTabStatusSet;

  void __fastcall Debug(String Message);
  bool __fastcall EventTabSync(void);

  void __fastcall SetEvTabStatus(CIIEventCode EvCode);
  void __fastcall MessageRecived();
  bool __fastcall ParseCommands();
  bool __fastcall TransferResponse();
  int  __fastcall Request( CIISCommand Cmd, CIISTable Tab, String Data);

  void __fastcall EventTabSyncIni();
  void __fastcall ProjectTabSyncIni();
  bool __fastcall ProjectTabSync();
  void __fastcall ProjectTabCeckIni();
  bool __fastcall ProjectTabCheck();
  void __fastcall SystemSyncSelectStep();
  void __fastcall CtrlTabSyncIni();
  bool __fastcall CtrlTabSync();
  void __fastcall ZoneTabSyncIni();
  bool __fastcall ZoneTabSync();
  void __fastcall SensorTabSyncIni();
  bool __fastcall SensorTabSync();
  void __fastcall PSTabSyncIni();
  bool __fastcall PSTabSync();
  void __fastcall RecordingTabSyncIni();
  bool __fastcall RecordingTabSync();
  void __fastcall ValueTabSyncIni();
  bool __fastcall ValueTabSync();
  void __fastcall ValueLPRTabSyncIni();
  bool __fastcall ValueLPRTabSync();
  void __fastcall ValueDecayTabSyncIni();
  bool __fastcall ValueDecayTabSync();
  void __fastcall CheckSyncIni();
  bool __fastcall CheckSync();
  void __fastcall LastValSyncIni();
  bool __fastcall LastValSync();
	void __fastcall SM_SendTransferMessage();
	void __fastcall SM_SystemSync();
	void __fastcall SM_DataSync();
	void __fastcall SM_LoggIn();
	void __fastcall SM_OpenCom();
	void __fastcall SM_CancelSync();
	void __fastcall SM_CloseCom();
	bool __fastcall SM_Running();

public:
			 __fastcall TCIISServerInterface( TCIISDBModule * SetDB,
																					 TDebugWin * SetDebugWin,
																					 String SetIP,
																					 int SetPortNo);
			 __fastcall ~TCIISServerInterface();
	void __fastcall SetTransferReply( TransferReplyFP SetTP );
	void __fastcall SetDebugWin(TDebugWin * SetDebugWin);
	bool __fastcall Connected();
	bool __fastcall TransferMessage( TCamurPacket *p );
	void __fastcall TimerSMEvent(TObject * Sender);
	void __fastcall TimerSMCancelEvent(TObject * Sender);
	void __fastcall TimerUpDateEvent(TObject * Sender);

	bool __fastcall LoggInIni( String SetUserLevel, String SetPassword );
	bool __fastcall SystemSyncIni();
	bool __fastcall DataSyncIni();
	bool __fastcall OpenComIni();
	bool __fastcall CancelSyncIni();
	bool __fastcall CloseComIni();
	String __fastcall StatusMessage();

__published:
		__property bool LoggedIn = { read=FLoggedIn };
		__property bool SysSyncRunning = { read=FSysSyncRunning };
		__property bool DataSyncRunning = { read=FDataSyncRunning};
		__property bool TransferMessageRunning = { read=FTransferMessageRunning };
		__property bool LoggInRunning = { read=FLoggInRunning };
		__property bool CancelSyncRunning = { read=FCancelSyncRunning };
		__property bool OpenComRunning = { read=FOpenComRunning };
		__property bool CloseComRunning = { read=FCloseComRunning };
};


#endif
